package com.skyline.wizard.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.springframework.jdbc.core.RowMapper;

public class ArticleDocMapper implements RowMapper<Document> {

	private static final String COLUMN_ID = "id";
	private static final String COLUMN_DIGEST = "digest";
	private static final String COLUMN_TITLE = "title";
	private static final String COLUMN_OWNER_ID = "ownerId";
	private static final String COLUMN_OWNER_NICKNAME = "ownerNickname";
	private static final String COLUMN_OWNER_PORTRAIT = "ownerPortrait";
	private static final String COLUMN_CATEGORY_ID = "categoryId";
	private static final String COLUMN_CATEGORY_NAME = "categoryName";

	private static ArticleDocMapper mapper = new ArticleDocMapper();

	public static ArticleDocMapper getMapper() {
		return mapper;
	}

	@Override
	public Document mapRow(ResultSet rs, int rowNum) throws SQLException {
		Document d = new Document();

		// 增加id
		Field f = new Field(COLUMN_ID, rs.getString(COLUMN_ID), Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加digest
		f = new Field(COLUMN_DIGEST, rs.getString(COLUMN_DIGEST), Store.YES, Index.ANALYZED);
		d.add(f);

		// 增加title
		f = new Field(COLUMN_TITLE, rs.getString(COLUMN_TITLE), Store.YES, Index.ANALYZED);
		d.add(f);

		// 增加owner id
		f = new Field(COLUMN_OWNER_ID, rs.getString(COLUMN_OWNER_ID), Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加owner nickname
		f = new Field(COLUMN_OWNER_NICKNAME, rs.getString(COLUMN_OWNER_NICKNAME), Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加owner portrait
		f = new Field(COLUMN_OWNER_PORTRAIT, rs.getString(COLUMN_OWNER_PORTRAIT), Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加category id
		f = new Field(COLUMN_CATEGORY_ID, rs.getString(COLUMN_CATEGORY_ID), Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		// 增加category name
		f = new Field(COLUMN_CATEGORY_NAME, rs.getString(COLUMN_CATEGORY_NAME), Store.YES, Index.NOT_ANALYZED);
		d.add(f);

		return d;
	}
}
